#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Log.hpp"
#include <string.h>


using namespace std;
Log lg;
// 提示用户命令行格式
void Usage(std::string proc)
{
    std::cout << "\n\rUsage: " << proc << " serverip serverport\n"
              << std::endl;
}

struct ThreadData
{
    struct sockaddr_in server;
    int sockfd;
    std::string serverip;
};

void *recv_message(void *args)
{
    // OpenTerminal();
    ThreadData *td = static_cast<ThreadData *>(args);
    char buffer[1024];
    while (true)
    {
        memset(buffer, 0, sizeof(buffer));
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);

        ssize_t s = recvfrom(td->sockfd, buffer, 1023, 0, (struct sockaddr *)&temp, &len);
        if (s > 0)
        {
            buffer[s] = 0;
            cerr << buffer << endl;
        }
    }
}
void *send_message(void *args)
{
    ThreadData *td = static_cast<ThreadData *>(args);
    string message;
    socklen_t len = sizeof(td->server);

    std::string welcome = td->serverip;
    welcome += " comming...";
    sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&(td->server), len);

    while (true)
    {
        cout << "Please Enter@ ";
        getline(cin, message);

        // std::cout << message << std::endl;
        // 1. 数据 2. 给谁发
        sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&(td->server), len);
    }
}

// ./udpclient serverip serverport
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(0);
    }

    std::string serverip = argv[1];          // 获取IP
    int16_t serverport = std::stoi(argv[2]); // 获取port,std::stoi(argv[2]) 将端口号从字符串转换为 uint16_t 类型。

    struct ThreadData td;
    bzero(&td.server, sizeof(td.server));
    td.server.sin_family = AF_INET; //IPV4
    td.server.sin_port = htons(serverport);  //port
    td.server.sin_addr.s_addr = inet_addr(serverip.c_str());  //IP

    //创建套接字
    td.sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (td.sockfd < 0)
    {
        cout << "socker error" << endl;
        return 1;
    }

    td.serverip = serverip;
    pthread_t recvr, sender;
    pthread_create(&recvr, nullptr, recv_message, &td);
    pthread_create(&sender, nullptr, send_message, &td);
    pthread_join(recvr, nullptr);
    pthread_join(sender, nullptr);

    close(td.sockfd);
    return 0;
}

// // ./udpclient serverip serverport
// int main(int argc, char *argv[])
// {
//     if (argc != 3)
//     {
//         Usage(argv[0]);
//         exit(0);
//     }
//     std::string serverip = argv[1];          // 获取IP
//     int16_t serverport = std::stoi(argv[2]); // 获取port,std::stoi(argv[2]) 将端口号从字符串转换为 uint16_t 类型。

//     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
//     if (sockfd < 0)
//     {
//         cout << "socker error" << endl;
//         return 1;
//     }

//     struct sockaddr_in server;
//     bzero(&server, sizeof(server));
//     server.sin_family = AF_INET;
//     server.sin_port = htons(serverport);
//     server.sin_addr.s_addr = inet_addr(serverip.c_str());
//     socklen_t len = sizeof(server);

//     string message;
//     char buffer[1024];
//     while (true)
//     {
//         cout << "Please Enter@ ";
//         getline(cin, message);
//         if (message.empty())
//             continue; // 跳过空输入

//         sendto(sockfd, message.c_str(), message.size(), 0, (const sockaddr *)&server, len);

//         struct sockaddr_in temp;
//         socklen_t temp_len = sizeof(temp); // 用不同变量名，避免覆盖

//         ssize_t s = recvfrom(sockfd, buffer, 1023, 0, (struct sockaddr *)&temp, &temp_len);
//         if (s < 0)
//         {
//             lg(Warning, "recvfrom error, errno: %d, err string: %s", errno, strerror(errno));
//         }
//         else
//         {
//             buffer[s] = 0;
//             std::cout << "Received: " << buffer << std::endl;
//         }
//     }
//     close(sockfd);
//     return 0;
// }